package com.canbot.u05.view;


import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.utils.Logger;

import java.util.ArrayList;
import java.util.Calendar;

public class TimePickerDialog extends Dialog {

        private final ArrayList<String> list_hour;
        private final ArrayList<String> list_min;

        private Context mContext;

        private Button positiveButton;

        private TextView title;

        private Calendar calendar;

        private String mHour;

        private String mMinute;

        private StringBuilder stringBuilder;

        private WheelView hourPicker;
        private WheelView minPicker;

        public TimePickerDialog(Context context) {
                super(context, R.style.MyDialogStyle);
                this.mContext = context;
                setCustomDialog();
                list_hour = new ArrayList<>();
                list_min = new ArrayList<>();
                for (int i=0;i<24;i++){
                        if (i<10){
                                list_hour.add("0"+i);
                        }else {
                                list_hour.add(i+"");
                        }

                }
                for (int i=0;i<60;i++){

                        if (i<10){
                                list_min.add("0"+i);
                        }else {
                                list_min.add(i+"");
                        }


                }

        }

        private void setCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_time_picker, null);
                title =  mView.findViewById(R.id.title);
                hourPicker =  mView.findViewById(R.id.hour);
                minPicker =  mView.findViewById(R.id.min);
                //初始化时间
                positiveButton =  mView.findViewById(R.id.sure);

                super.setContentView(mView);


        }

        public void setTitle(String text) {
                title.setText(text);
        }

        public void setTime(String hour, String minute) {
                //设置时间
                //设置原始数据
                hourPicker.setData(list_hour);
                for (int i = 0; i < list_hour.size(); i++) {
                        if (list_hour.get(i).equals(hour) ) {
                                hourPicker.setDefault(i);
                        }
                }

                minPicker.setData(list_min);
                for (int i = 0; i < list_min.size(); i++) {
                        if (list_min.get(i).equals(minute)  ){
                                minPicker.setDefault(i);
                        }
                }
        }



        @Override
        public void setContentView(int layoutResID) {
        }

        @Override
        public void setContentView(View view, LayoutParams params) {
        }

        @Override
        public void setContentView(View view) {
        }


        /**
         * 确定键监听器
         *
         * @param listener
         */
        public void setOnPositiveListener(View.OnClickListener listener) {
                positiveButton.setOnClickListener(listener);

        }


        public String getTime() {
                mHour = hourPicker.getSelectedText();
                mMinute = minPicker.getSelectedText();
                return new StringBuilder().append(mHour).append(":")
                        .append(mMinute).toString();
        }

}
